using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using UMC.Data;
using UMC.Net;
using UMC.Web;
using System.Collections;

namespace UMC.ITME
{
   
    class WebClient : UMC.Web.WebClient
    {

        List<WebMeta> uievents = new List<WebMeta>();
        public WebClient(NetContext context) : base(context)
        {
            this.RedirectTimes = 0;
        }
        protected override void OutputCheck(WebContext context)
        {
            if ((context.Response.ClientEvent & WebEvent.UIEvent) == WebEvent.UIEvent)
            {
                context.Response.ClientEvent = context.Response.ClientEvent ^ WebEvent.UIEvent;
                var uiEvent = context.Response.Headers.GetMeta("UIEvent");
                context.Response.Headers.Remove("UIEvent");

                if (uiEvent.ContainsKey("Submit"))
                {
                    uiEvent.Put("Type", "UI.Event");
                    uievents.Add(uiEvent);
                    WebMeta click = uiEvent.GetMeta("Click");//["Click"];
                    uiEvent.Remove("Click");
                    if (click != null)
                    {
                        var m = click["model"];
                        var c = click["cmd"];
                        var send = click.GetMeta("send");

                        context.Response.Redirect(m, c, send, false);
                        return;
                    }
                }
                else if (uiEvent.ContainsKey("key"))
                {
                    var key = uiEvent["key"];
                    var web = uievents.Find(r => r["Key"] == key);

                    if (web != null)
                    {
                        if (uiEvent.ContainsKey("value"))
                        {
                            WebMeta Submit = web.GetMeta("Submit");//["Click"];
                            var Name = web["Name"];
                            var m = Submit["model"];
                            var c = Submit["cmd"];
                            var send = Submit.GetMeta("send") ?? new WebMeta();
                            uievents.Remove(web);
                            Object value = uiEvent.GetDictionary()["value"];
                            if (value is ListItem)
                            {
                                var item = value as ListItem;
                                send.Put(Name, item.Value);
                                send.Put($"{Name}_Text", item.Text);
                            }
                            else if (value is Hashtable)
                            {
                                var item = value as Hashtable;
                                send.Put(Name, item["Value"]);
                                send.Put($"{Name}_Text", item["Text"]);
                            }
                            else
                            {
                                send.Put(Name, value);
                            }
                            context.Response.Redirect(m, c, send, false);
                            return;
                        }
                    }
                }
                context.Send("UI.Event", uiEvent, false);

            }

            if (uievents.Count > 0)
            {
                var ac = context.Response.Headers.GetMeta("AsyncDialog");
                if (ac != null)
                {
                    uievents.Add(ac);
                }
                context.Response.Headers.Put("AsyncDialog", uievents.ToArray());
                context.Response.ClientEvent |= WebEvent.AsyncDialog;
            }
        }
    }
}